package com.jcfk.eam.service.main;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.eam.dao.main.EamCheckAnomalyTrackingDao;
import com.jcfk.eam.domain.dto.main.EamCheckAnomalyTrackingDTO;
import com.jcfk.eam.domain.po.base.EamRepairOrder;
import com.jcfk.eam.domain.po.main.EamPointCheckDetail;
import com.jcfk.eam.domain.query.main.EamCheckAnomalyTrackingQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 巡点检异常跟踪服务
 */
@Service
public class EamCheckAnomalyTrackingService {


    @Resource
    private EamCheckAnomalyTrackingDao eamCheckAnomalyTrackingDao;

    @Resource
    private EamPointCheckDetailService eamPointCheckDetailService;


    public DataPage<EamCheckAnomalyTrackingDTO> queryByPage(DataPage<EamCheckAnomalyTrackingDTO> page, EamCheckAnomalyTrackingQuery query) {

        if (StrUtil.isNotEmpty(query.getAssetInfo())) query.setAssetInfo("%" + query.getAssetInfo() + "%");
        // 分页查询
        page = this.eamCheckAnomalyTrackingDao.queryByPage(page, query);
        return page;
    }

    /**
     * 处理异常跟踪
     * @param pointCheckDetailId 异常详情ID
     * @param item 故障保修单
     */
    @Transactional
    public void handle(String pointCheckDetailId, EamRepairOrder item) {
        String[] split = pointCheckDetailId.split(",");
        Assert.notEmpty(split, "异常详情不能为空");
        List<String> pointCheckDetailIds = Arrays.asList(split);
        List<EamPointCheckDetail> details = eamPointCheckDetailService.list(Wrappers.<EamPointCheckDetail>lambdaQuery()
            .in(EamPointCheckDetail::getId, pointCheckDetailIds));
//        EamPointCheckDetail checkDetail = eamPointCheckDetailService.getById(pointCheckDetailId);
        Assert.notEmpty(details, "异常详情不存在");
        details.forEach(checkDetail -> {
            Assert.isTrue(3 != checkDetail.getExceptionHandleStatus(), "异常详情处理状态: 已关闭");
            checkDetail.setExceptionHandleStatus(2);
            checkDetail.setHandleMode(1);
            checkDetail.setHandleContent(StrUtil.format("故障维修:{}", item.getRepairCode()));
            checkDetail.setHandleUserId(item.getRepairUser());
            checkDetail.setHandleUserName(item.getRepairUserName());
            checkDetail.setHandleTime(DateUtil.date());
            checkDetail.setRepairOrderId(item.getId());
            checkDetail.setRepairOrderNo(item.getRepairCode());
        });
        eamPointCheckDetailService.updateBatchById(details);
    }

    public void close(String checkDetailId, String remarks) {
        EamPointCheckDetail checkDetail = eamPointCheckDetailService.getById(checkDetailId);
        Assert.notNull(checkDetail, "异常详情不存在");
        Assert.isTrue(1 == checkDetail.getExceptionHandleStatus(), "异常详情处理状态: {}, 不可关闭", checkDetail.getExceptionHandleStatus() == 2 ? "处理中" : "已关闭");
        checkDetail.setExceptionHandleStatus(3);
        checkDetail.setHandleMode(3);
        checkDetail.setHandleContent(StrUtil.format("关闭异常  原因:{}", remarks));
        checkDetail.setHandleUserId(SecurityUtils.getUserId());
        checkDetail.setHandleUserName(SecurityUtils.getUserName());
        checkDetail.setHandleTime(DateUtil.date());
        eamPointCheckDetailService.updateById(checkDetail);
    }

    @Transactional
    public void finished(String repairOrderId, String repairStatus, String memo) {
        List<EamPointCheckDetail> details = eamPointCheckDetailService.list(Wrappers.<EamPointCheckDetail>lambdaQuery()
            .eq(EamPointCheckDetail::getRepairOrderId, repairOrderId));
        if (CollUtil.isEmpty(details)) return;
        details.forEach(checkDetail -> {
            checkDetail.setExceptionHandleStatus(3);
            checkDetail.setHandleUserId(SecurityUtils.getUserId());
            checkDetail.setHandleUserName(SecurityUtils.getUserName());
            checkDetail.setHandleTime(DateUtil.date());
            checkDetail.setHandleContent(StrUtil.format("{}, 维修单处理结果: {}",
                checkDetail.getHandleContent(),
                repairStatus.equals("4") ? "确认完成"
                    : repairStatus.equals("5") ? "故障取消"
                    : repairStatus.equals("6") ? StrUtil.format("故障关闭, 关闭原因:{}", memo)
                    : "")
            );
        });
        eamPointCheckDetailService.updateBatchById(details);
    }
}
